//
//  UIView+Animation.m
//  DSJY
//
//  Created by 冯华强 on 16/10/21.
//  Copyright © 2016年 dsjy. All rights reserved.
//

#import "UIView+Animation.h"
#import "UIView+Extension.h"

@implementation UIView (Animation)

- (void)positionLayerView:(UIView *)layerView toView:(UIView *)toView delegateVC:(UIViewController *)delegateVC
{
    [self positionLayerView:layerView toView:toView delegateVC:delegateVC duration:1.0];
}

- (void)positionLayerView:(UIView *)layerView toView:(UIView *)toView delegateVC:(UIViewController *)delegateVC duration:(CFTimeInterval)duration
{
    //再创建动画
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position";
    
    //开始的value
    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(layerView.centerX, layerView.centerY)];
    
    NSInteger vInnerBgMargin = 10;
    //到达的value
    CGRect frame = [toView convertRect:toView.bounds toView:self];
    CGFloat y = frame.origin.y + layerView.height * 0.5 + vInnerBgMargin;
    CGFloat x = frame.origin.x + layerView.width * 0.5 + vInnerBgMargin;
    NSValue *toValue = [NSValue valueWithCGPoint:CGPointMake(x, y)];
    anim.toValue = toValue;
    
    //时间
    anim.duration = duration;
    anim.delegate = delegateVC;
    anim.timingFunction =
    [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    [layerView.layer addAnimation:anim forKey:nil];
}

//内容专场动画
- (void)contentTransitionWithContentView:(UIView *)contetnView delegateVC:(UIViewController *)delegateVC
{
    [self contentTransitionWithContentView:contetnView delegateVC:delegateVC duration:0.5];
}

//内容专场动画
- (void)contentTransitionWithContentView:(UIView *)contetnView delegateVC:(UIViewController *)delegateVC duration:(CFTimeInterval)duration
{
    //先赋值，再执行转场动画
    contetnView = self;
    
    CATransition *tran = [CATransition animation];
    //动画过渡类型
    tran.type = kCATransitionPush;
    //动画过渡方向
    tran.subtype = kCATransitionFromRight;
    //动画结束位置
    tran.endProgress = 1;
    tran.duration = duration;
    tran.delegate = delegateVC;
    [contetnView.layer addAnimation:tran forKey:nil];
}

/**
 *  键盘关闭
 */
- (void)keyboardWillHide:(NSNotification *)noti coverView:(UIView *)coverView
{
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    coverView.hidden = YES;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration animations:^{
        weakSelf.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {
        weakSelf.hidden = YES;
    }];
}

//有输入框输入状态的现实视图移动
- (void)keyboardWillTransform:(NSNotification *)noti eventViewTop:(CGFloat)eventViewTop isHide:(BOOL)isHide
{
    [self keyboardWillTransform:noti eventViewTop:eventViewTop keyToolH:0 isHide:isHide];
}

//有输入框输入状态的现实视图移动
- (void)keyboardWillTransform:(NSNotification *)noti eventViewTop:(CGFloat)eventViewTop keyToolH:(CGFloat)keyToolH isHide:(BOOL)isHide
{
    CGRect endFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    __weak typeof(self) weakSelf = self;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    NSInteger transForm =  (screenH - eventViewTop - 64 - endFrame.origin.y - keyToolH);
    if (transForm >= 0) return;
    if (isHide) transForm = 0;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.transform = CGAffineTransformMakeTranslation(0, transForm);
    }];
}

@end
